package com.project.running.Service.Impl;

import com.project.running.Service.GraderecordService;
import com.project.running.Service.PointactionService;
import com.project.running.Service.registerService;

import com.project.running.bean.Memberinfo;
import com.project.running.bean.Pointaction;
import com.project.running.bean.Pointrecord;
import com.project.running.dao.GraderecordMapper;
import com.project.running.dao.MemberinfoMapper;
import com.project.running.dao.PointactionMapper;
import com.project.running.dao.PointrecordMapper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/10/5-10-05-18:14
 * @Description：com.project.running.Service.Impl
 */

@Service
public class RegisterImpl implements registerService {

    @Autowired
    MemberinfoMapper miMapper;
    @Autowired
    PointactionMapper pcMapper;
    @Autowired
    GraderecordMapper gcMapper ;
    @Autowired
    PointrecordMapper ptcMapper ;

    @Override
    public String register(Memberinfo member,String authCode, String verifyCode) {
//        SqlSession session = SqlSessionUtil.openSession();
//        MemberinfoMapper miMapper = session.getMapper(MemberinfoMapper.class);
//        PointactionMapper pcMapper = session.getMapper(PointactionMapper.class);
//        GraderecordMapper gcMapper = session.getMapper(GraderecordMapper.class);
//        PointrecordMapper ptcMapper = session.getMapper(PointrecordMapper.class);

        if(authCode == null || "".equals(authCode.trim())){
            return "请输入验证码！";
        }else {
            if(verifyCode.length() != authCode.length()) {
                return "验证码输入错误！";
            }
            for (int i = 0; i < verifyCode.length(); i++) {
                if(verifyCode.charAt(i) != authCode.charAt(i)){
                    return "验证码输入错误！";
                }
            }
        }

       if(miMapper.findByName(member.getNickname()) != null){
           return "用户名已存在";
       }
        System.out.println(member.getRecommender());
       if(member.getRecommender()!= null && !"".equals(member.getRecommender())){                              //若存在推荐人，需要更新推荐人积分
           Memberinfo memberinfo = miMapper.selectByPrimaryKey(Long.parseLong(member.getRecommender()));
           if(memberinfo == null){                                      //根据id，若不存在该推荐人，则报错
               return "该推荐人不存在，请重新输入";
           }else{
               Pointrecord pt = new Pointrecord();

               Pointaction recommend = pcMapper.selectByName("RECOMMEND");
               memberinfo.setPoint(memberinfo.getPoint()+recommend.getPoint());
               Long point = member.getPoint();                 //更新推荐人积分

               memberinfo.setGradeid(gcMapper.selectByPoint(point).getId()); //更新推荐人的积分等级

               pt.setNickname(memberinfo.getNickname());
               pt.setReceivedate(new Date(System.currentTimeMillis()));
               pt.setPointactionid(recommend.getId());

               ptcMapper.insert(pt);
               miMapper.updateByPrimaryKey(memberinfo);              //更新推荐人信息
           }
       }
        System.out.println(member);
        int insert = miMapper.insert(member);                         //注册用户信息
        if(insert != 1){
            return "注册失败";
        }

//        session.commit();                    //事务提交
//        session.close();
        return "注册成功";
    }
}
